﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Security;
using David.Framework.Core.Enums;
using David.Framework.Core.Helper;
using David.Framework.Core.Security;
using David.Framework.Web.Filter;
using David.Framework.Web.Security;
using David.FrameWork.Web.Controllers;
using David.WebSite.Data;
using David.WebSite.Web.Models;
using David.WebSite.Web.Models.API;

namespace David.WebSite.Web.Controllers.API
{
    [ExceptionHandler]
    public class LoginApiController : BaseApiController
    {
        /// <summary>
        /// 登陆api
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<object> Login(LoginRequest request)
        {
            var response = new Response<object>();
            if (string.IsNullOrEmpty(request.userName))
            {
                response.data = new { result = false, errorcode = 1 };
                return response;
            }
            if (string.IsNullOrEmpty(request.passWord))
            {
                response.data = new { result = false, errorcode = 2 };
                return response;
            }

            var loginResult = AdminsDataService.LoginValidation(request.userName, Encryption.SHA256Encrypt(Encryption.MD5(request.passWord)));
            if (loginResult == null)
            {
                response.data = new { result = false, errorcode = 3 };
                return response;
            }
            try
            {
                LogDataService.Log("登陆了系统", LogType.AdminsLoginInfo, $"userid={loginResult.UserId};userIp={IPHelper.GetClicentIp()};userMacAddress={CommonTool.GetMacAddress()};", CurrentUserId);
            }
            catch (Exception ex)
            {
                LogDataService.Exception("记录登录用户信息异常", ex.Message, ex.Source, $"userid={loginResult.UserId}");
            }
            AdminsDataService.UpdateLoginInfo(loginResult.UserId, CommonTool.getIP());
            DavidFormsAuthentication.SignIn(loginResult.UserName, loginResult.UserId, true);

            response.data = new { result = true, token = loginResult.UserId };
            return response;
        }
        /// <summary>
        /// 退出 api
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<object> Logout()
        {
            var result = new Response<object>();
            DavidFormsAuthentication.SignOut();
            result.data = new { result = true };
            return result;
        }

        /// <summary>
        /// 获取个人信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [LoggedApi]
        public Response<object> GetUserInfo()
        {
            var result = new Response<object>();
            if (CurrentUserId == 0)
            {
                result.data = null;
                return result;
            }
            var userInfo = AdminsDataService.GetUseInfoByUserId(CurrentUserId);
            string[] userRoles = null;
            switch ((AdminRole)userInfo.UserRole)
            {
                case AdminRole.System:
                    userRoles = new[] { "super_admin", "admin" };
                    break;
                case AdminRole.Admin:
                    userRoles = new[] { "admin" };
                    break;
                case AdminRole.Editor:
                    userRoles = new[] { "editor" };
                    break;
                default:
                    userRoles = new[] { "" };
                    break;
            }
            result.data = new { userName = userInfo.UserName, userId = userInfo.UserId, roles = userRoles };
            return result;
        }

    }
}
